<?php
/**
 * Created by PhpStorm.
 * User: adminstrator
 * Date: 2018/2/8
 * Time: 16:35
 */
header("Content-type:text/html;charset=utf-8");
date_default_timezone_set('PRC');
include_once("./Db_pdo.class.php");
echo '<pre>';
$DB=new Db_pdo("cxh_v3","cxhv3","cxhv3pwd!@#");
// $DB=new Db_pdo("root","root","jmshop");

ini_set("max_execution_time", 0);
// echo 222;
// exit;
// $sql = "SELECT
// *
// FROM
// ims_cxh_apply_subsidy_car
// WHERE
// month_now = 201903
// and aplc_status =1
// and id >20
// HAVING aplc_totel_balance +aplc_re_money != aplc_totel_money";

// $datas= $DB->new_query($sql);
// $datas = $datas->fetchAll(PDO::FETCH_ASSOC); 
// // print_r($datas);

// foreach($datas as $key => $value){
// 	$s = $value['aplc_totel_money'] - $value['aplc_totel_balance'];
	
// 	$sql = 'update ims_cxh_apply_subsidy_car set aplc_re_money='.$s.' where id='.$value['id'];
// 	// echo $sql;
// 	// echo '<br>';
// 	// $sqls = 'DELETE from ims_cxh_apply_subsidy_back  WHERE id   ='.$value['id'];
// 	// $DB->db->query($sqls);
// 	// // echo $sqls;
// 	// // echo '<br>';
// 	// $sqls1 = "DELETE FROM ims_cxh_cash_log WHERE cash_bus_id  = ".$value['id']. ' and ctime = 1546453202';
// 	$DB->db->query($sql);
// 	// echo $sqls1;

// } 

// $id = 1329;
// //车辆补贴
// $field = 'su.ctime,su.id,su.aplc_totel_retime,su.aplc_totel_money,su.aplc_totel_days,su.aplc_totel_balance,su.aplc_re_money,su.aplc_re_money_p,su.aplc_re_integral,su.aplc_re_integral_p,su.aplc_re_fee,su.aplc_re_fee_p,su.member_id,me.credit2,me.credit1,me.cxhname';
// $sql = 'select '.$field.' from ims_cxh_apply_subsidy_car as su inner join ims_ewei_shop_member as me on su.member_id=me
// 	.id where su.aplc_status=1 and su.is_all_back=1 and su.is_month_back=1 and su.member_id  not IN (
// 		4820,
// 		4807,
// 		1723,
// 		3008,
// 		4819,
// 		4807,
// 		4844,
// 		4818,
// 		1352,
// 		4733,
// 		4587,
// 		4673,
// 		4765,
// 		4691,
// 		4587,
// 		4835,
// 		4833,
// 		4838,
// 		4839
// 	)';

// $datas= $DB->new_query($sql);
// $datas = $datas->fetchAll(PDO::FETCH_ASSOC); 
// $time = 1546453202;
// // $endtime = 1534867244;
// // $st = ceil(($endtime-$time)/86400);
// print_r($datas);


// exit;
// // // echo $st;
// // for($i =1;$i<=$st;$i++){
// 		$fields = 'su.ctime,su.id,su.aplc_totel_retime,su.aplc_totel_money,su.aplc_totel_days,su.aplc_totel_balance,su.aplc_re_money,su.aplc_re_money_p,su.aplc_re_integral,su.aplc_re_integral_p,su.aplc_re_fee,su.aplc_re_fee_p,su.member_id,me.credit2,me.credit1,me.cxhname';
// 		$sqls = 'select '.$fields.' from ims_cxh_apply_subsidy_car as su inner join ims_ewei_shop_member as me on su.member_id=me
// 	.id where su.aplc_status=1 and su.is_all_back=1 and su.is_month_back=1 and su.id='.$id;

// 		$data= $DB->new_query($sqls);
// 		$data = $data->fetchAll(PDO::FETCH_ASSOC); 
// 		$nowtime = $time +86400*$i;
// 		$nowtimes =date('Y-m-d',$nowtime);
		
		
// 		if($data){

// 			$newarrs = array();
// 			foreach ($data as $key => $value) {
// 				//判断已经返还完的
// 				if($value['aplc_totel_money']>$value['aplc_re_money']){
// 					//每天返款总金额
// 					$money = round($value['aplc_totel_balance']/($value['aplc_totel_days']-$value['aplc_totel_retime']),2);
// 					// echo $money;
// 					//剩余金额(防止返款不足的情况)
// 					if($value['aplc_totel_balance']<$money){
// 						$param['aplc_totel_balance'] = 0;
// 						$money = $value['aplc_totel_balance'];
// 					}else{
// 						$param['aplc_totel_balance'] = $value['aplc_totel_balance']-$money;
// 					}


// 					// echo $money;
// 					//金额(判断是否全返款还是其他)
// 					$fee_money =0;
// 					if($value['aplc_re_fee_p']>0){
// 						$fee_money = round($money*$value['aplc_re_fee_p'],2);
// 						$balance_money =  round($money-$fee_money,2);
// 					}else{
// 						$balance_money = $money;
// 					}


// 					//已返款天数
// 					$param['aplc_totel_retime'] = $value['aplc_totel_retime']+1;
// 					//已返金额
// 					$param['aplc_re_money']  = $value['aplc_re_money']+$money;
// 					//已返维护费
// 					$param['aplc_re_fee'] = $value['aplc_re_fee'] +$fee_money;
// 					// echo $balance_money;
// 					// print_r($param);
// 					$condition['id'] = $value['id'];
// 					//处理申请表
// 					$DB->table("ims_cxh_apply_subsidy_car")->where("id=".$value['id'])->update($param);
					

// 					//添加member表的钱
// 					$params = 'credit2=credit2+'.$balance_money.', credit3=credit3-'.$balance_money;
// 					$condition1['id'] = $value['member_id'];
					
// 					$es = 'update ims_ewei_shop_member set credit2=credit2+'.$balance_money.',credit3=credit3-'.$money.' where id ='.$value['member_id'];
// 					// echo $es;
// 					// exit;
// 					 $tt = $DB->db->query($es);

// 					// echo 999;

// 					// //back表记录
// 				    $log['apb_type'] = 1;
// 					$log['bus_id'] = $value['id'];

// 					$log['apb_money'] = $balance_money;
// 					$log['apb_fee'] = $fee_money;
// 					$log['member_id'] = $value['member_id'];
// 					$log['apb_intgral'] = 0;
// 					$log['ctime'] =$nowtime;

				
// 					$DB->table("ims_cxh_apply_subsidy_back")->insert($log);
// 					//一个用户拥有多个车辆的情况

// 					$old = $value['credit2'];
// 					if(isset($newarrs[$value['member_id']])){
// 						$old = $newarrs[$value['member_id']];
// 					}

// 					// //一总收入
// 					$tol_log['cash_bus_type'] = 1;
// 					$tol_log['cash_type'] = 1;
// 					$tol_log['cash_old'] = $old;
// 					$tol_log['cash_now'] = $money+$old;
// 					$tol_log['cash_num'] = '+'.$money;
// 					$tol_log['remark'] = '[车辆补贴返款] 补贴总金额 '.$nowtimes;
// 					$tol_log['member_id'] = $value['member_id'];
// 					$tol_log['ctime'] =$nowtime;
					
// 					$DB->table("ims_cxh_cash_log")->insert($tol_log);

// 					if($value['aplc_re_fee_p']>0){
// 						//平台费
// 						$fee_log['cash_bus_type'] = 1;
// 						$fee_log['cash_type'] = 1;
// 						$fee_log['cash_now'] = $money+$old-$fee_money;
// 						$fee_log['cash_old'] = $money+$old;
// 						$fee_log['cash_num'] = '-'.$fee_money;
// 						$fee_log['remark'] = '[车辆补贴返款] 扣除平台维护费 ' .$nowtimes;
// 						$fee_log['member_id'] = $value['member_id'];
// 						$fee_log['ctime'] =$nowtime;
// 						$DB->table("ims_cxh_cash_log")->insert($fee_log);
						
// 					}

// 					if($value['aplc_re_fee_p']>0){
// 						$newarrs[$value['member_id']] = $balance_money+$old;
// 					}else{
// 						$newarrs[$value['member_id']] = $money+$old;
// 					}

					
// 				}

// 			}
// 			// unset($newarrs);
// 		}
// // }

// 车补处理mmeberuuid没有的 

// $sql = "SELECT
// a.member_id,
// b.realname,
// b.member_uuid,
// b.id,
// a.member_uuid as uid
// FROM
// ims_cxh_apply_subsidy_car AS a
// INNER JOIN ims_ewei_shop_member AS b ON a.member_id = b.id
// WHERE
//  a.member_uuid = ''";

// $data= $DB->new_query($sql);
// $data = $data->fetchAll(PDO::FETCH_ASSOC); 
// file_put_contents('carss.txt',serialize($data));

// $data = unserialize(file_get_contents('carss.txt')); 
// // print_r($data);
// // exit;
// foreach($data as $v){
//     $sql = 'UPDATE ims_cxh_apply_subsidy_car set member_uuid = "'.$v['member_uuid']. '" WHERE member_id ='.$v['id'];
//     // $sqls = "UPDATE ims_cxh_apply_subsidy_car set member_uuid =".$v['member_uuid']. "WHERE member_id =" .$v['id'].  "and aplc_status =1";
//     $DB->new_query($sql);
//     // echo $sql;

// }
// print_r($data);

 






















 